package com.yanqu.road.server.cmd.wingroom.workshop;

import com.yanqu.road.entity.wingroom.workshop.WorkShopBuyCount;
import com.yanqu.road.pb.wingroom.WingRoomProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.workshop.WorkShopModule;
import com.yanqu.road.server.pb.WingRoomPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;

/**
 * @author ：zfr
 * @date ：2022/2/25 18:01
 */
@Cmd(code = GameProtocol.S_WING_ROOM_WORK_SHOP_BUY, desc = "厢房工坊购买")
public class WorkShopBuyCmd extends AbstractCommand {
    private int furnitureId;
    private long count;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        WingRoomProto.WorkShopBuyReqMsg msg = WingRoomProto.WorkShopBuyReqMsg.parseFrom(packet.getBytes());
        furnitureId = msg.getFurnitureId();
        count = msg.getBuyCount();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ret = player.getModule(WorkShopModule.class).workShopBuyFurniture(furnitureId, count);
    }

    @Override
    public void sendData(GamePlayer player) {
        WingRoomProto.WorkShopBuyRespMsg.Builder respMsg = WingRoomProto.WorkShopBuyRespMsg.newBuilder();
        respMsg.setRet(ret);
        WorkShopBuyCount buyCount = player.getModule(WorkShopModule.class).getWorkShopBuyCount(furnitureId);
        if (buyCount != null) {
            respMsg.setBuyCount(WingRoomPb.buildWorkShopBuyCountTemp(buyCount));
        }
        player.sendPacket(ClientProtocol.U_WING_ROOM_WORK_SHOP_BUY, respMsg);
    }
}
